package com.example.travel.controller;

import com.example.travel.pojo.ResultType;
import com.example.travel.pojo.orm.User;
import com.example.travel.pojo.result.UserLoginResult;
import com.example.travel.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController()
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/user/login_register")
    public UserLoginResult userLoginRegister(@RequestBody User user){
        System.out.println(user.toString());
        User userByName = userService.getUserByName(user.getUserName());
        System.out.println(userByName==null);
        if (userByName==null){
            User lastUser = userService.getLastUser();
            userService.addUser(user);
            return new UserLoginResult(ResultType.SUCCESS,false,lastUser.getUserId()+1);
        }else {
            User user1 = userService.checkPassword(user);
            System.out.println("user1="+user1);
            if (user1==null){
                return new UserLoginResult(ResultType.FAIL,true,userByName.getUserId());
            }else {
                return new UserLoginResult(ResultType.SUCCESS,false,userByName.getUserId());
            }
        }
    }

    @GetMapping("/user/get_user_by_id/{userId}")
    public User getUserById(@PathVariable("userId")Integer userId){
        System.out.println("请求获取用户信息");
        return userService.getUserById(userId);
    }

    @GetMapping("/user/get_user_by_like_name")
    public List<User> getUserByLikeName(@RequestParam("userName")String userName){
        System.out.println("模糊查询用户");
        return userService.getUserByLikeName(userName);
    }



}
